package model.semantics;

import model.State;
import model.parser.ParserException;

/**
 * Represents the to expression. Like assignment node but doesn't actually evaluate.
 * @author Sterling Dorminey
 */
public class ToNode extends Node<Integer> {
	private String myLabel;
	private Node<Integer> myChild;
	
	/**
	 * Takes a left node, whose label is to be used as the key,
	 * and a right node, which is the reference to which the key will point.
	 */
	public ToNode(Node<Integer> labelNode, Node<Integer> child) {
		// Can only construct an assignment if the left side is a variable node.
		if(!(labelNode instanceof VariableNode)) {
			throw ParserException.BAD_ARGUMENTS;
		}
		
		myLabel = labelNode.toString();
		myChild = child;
	}
	
	@Override
	public Integer evaluate(State<Integer> state) {
		state.setAssignment(myLabel, myChild);
		return 0;
	}

	@Override
	public String toString() {
		return "(=" + myLabel + " " + myChild.toString()+ ")";
	}
}